
#include "DIJoystick.h"

CDIJoystick:: CDIJoystick()
{
	memset( &m_State,0,sizeof(DIJOYSTATE2) );
	memset( &m_StateLast,0,sizeof(DIJOYSTATE2) );
}

CDIJoystick:: ~CDIJoystick()
{
}

bool CDIJoystick::Init( HWND hWnd )
{
	m_pDevice->SetDataFormat(&c_dfDIJoystick2);
	m_pDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE);
	m_pDevice->Acquire();

	m_hWnd = hWnd;

	return true;
}

bool CDIJoystick::Update()
{
	if(!m_pDevice)
		return false;

	HRESULT hr = m_pDevice->Acquire();
	if(FAILED(hr))
	{
		return false;
	}
	else
	{
		memcpy( &m_StateLast,&m_State,sizeof(DIJOYSTATE2) );
		m_pDevice->GetDeviceState( sizeof(DIJOYSTATE2),&m_State);
	}

	return true;
}


bool CDIJoystick::IsButtonPressed( int btn )
{
	BYTE stat = m_State.rgbButtons[btn];

	return ButtonPressed( stat ) ? true:false;
}

bool CDIJoystick::IsButtonUp( int btn )
{
	BYTE stat = m_State.rgbButtons[btn];
	BYTE stat_last = m_StateLast.rgbButtons[btn];

	return ButtonUp( stat,stat_last ) ? true:false;
}

bool CDIJoystick::IsButtonDown( int btn )
{
	BYTE stat = m_State.rgbButtons[ btn ];
	BYTE stat_last = m_StateLast.rgbButtons[btn];

	return ButtonDown( stat,stat_last ) ? true:false;
}

LONG CDIJoystick::GetAxisX()
{
	return m_State.lX;
}

LONG CDIJoystick::GetAxisY()
{
	return m_State.lY;
}

LONG CDIJoystick::GetAxisZ()
{
	return m_State.lZ;
}

int CDIJoystick::GetButtonCount()
{

	int nPushButtons = GetObjectCount( DIDFT_PSHBUTTON );
	return nPushButtons;
}